package com.gitee.ice1938.util;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;

public abstract class JsonTool {

	public static String toJson(Object obj) {
		return toJson(obj, false);
	}

	public static Map<?, ?> toMap(String jsonStr) {
		return json2Map(jsonStr);
	}

	/**
	 * java对象和JSON结构的转换类
	 */
	private static final ObjectMapper OBJECT_MAPPER;

	/**
	 * 初始化json和object映射关系对象，并指定序列化和反序列化的日期的格式
	 */
	static {
		OBJECT_MAPPER = new ObjectMapper();
		OBJECT_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	}

	/**
	 * 获取java对象和JSON结构的转换类
	 * 
	 * @return
	 */
	public static ObjectMapper getObjectMapper() {
		return OBJECT_MAPPER;
	}

	/**
	 * JSON串转换为Java泛型对象，可以是各种类型。
	 * 
	 * @param <T>
	 *            java对象的类型
	 * @param jsonString
	 *            JSON字符串
	 * @param tr
	 *            TypeReference,例如: new TypeReference< List<FamousUser> >(){}
	 * @return List对象列表
	 */
	public static <T> T jsonToGenericObject(String jsonString, TypeReference<T> tr) {
		if (jsonString == null || jsonString.isEmpty()) {
			return null;
		} else {
			try {
				return OBJECT_MAPPER.readValue(jsonString, tr);
			} catch (Exception e) {
				throw new RuntimeException(e.getMessage(), e);
			}
		}
	}

	/**
	 * Java对象转JSON字符串
	 * 
	 * @param object
	 *            Java对象，可以是对象，数组，List,Map等
	 * @return JSON 字符串
	 */
	public static String toJson(Object object, boolean isPretty) {
		String jsonString;
		try {
			if (isPretty) {
				jsonString = OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(object);
			} else {
				jsonString = OBJECT_MAPPER.writeValueAsString(object);
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
		return jsonString;

	}

	/**
	 * JSON字符串转Java对象
	 * 
	 * @param jsonString
	 *            要转换的JSON字符串
	 * @param c
	 *            java类型
	 * @return
	 */
	public static <T> T jsonToObject(String jsonString, Class<T> c) {
		try {
			if (jsonString == null || jsonString.isEmpty()) {
				return c.newInstance();
			} else {
				return OBJECT_MAPPER.readValue(jsonString, c);
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	/**
	 * 将Json字符串转换成map
	 * 
	 * @author zhaogang
	 */
	public static Map<String, Object> json2Map(String jsonString) {
		try {
			if (jsonString == null || jsonString.isEmpty()) {
				return null;
			}
			@SuppressWarnings("unchecked")
			Map<String, Object> map = OBJECT_MAPPER.readValue(jsonString, Map.class);
			return map;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	/**
	 * null值的转换方式定义
	 * 
	 * @author you
	 * 
	 */
	public static class NullSerializer extends JsonSerializer<Object> {
		@Override
		public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
			jgen.writeString((String) null);
		}
	}

}
